home *** CD-ROM | disk | FTP | other *** search
- Path: news.compuserve.com!newsmaster
- From: Philippe Verdy <100105.3120@compuserve.com>
- Newsgroups: comp.lang.c++
- Subject: Re: What are the differences between structures and classes in C++ ?
- Date: 7 Apr 1996 21:41:55 GMT
- Organization: CompuServe Incorporated
- Message-ID: <4k9cr3$fl9@arl-news-svc-5.compuserve.com>
- NNTP-Posting-Host: ad04-110.compuserve.com
-
- marnold@netcom.com (Matt Arnold) s'Θcrit :
- > Raghuveera Ravinutala <raghur> writes:
- >
- > ><jtbell@presby.edu (Jon Bell)> wrote :
- > >>the only *formal* difference between a class and a
- > >>struct is that by default, class members are private whereas struct
- > >>members are public.
- >
- > >Thanx. I am aware of this difference. I would like to know more, like,
- > >inheritence etc..
- >
- > There is none, other the default protection state of members. This is
- > what the original poster meant by only a formal difference. All the
- > rules you know about classes in C++ also apply to structs. For example,
- > the following two versions of Foo are precisely equivalent...
- >
- > // version #1, using class keyword
- >
- > class Foo: public Bar
- > {
- > public:
- > // some public members
- > private:
- > // some private members
- > };
- >
- > // version #2, using struct keyword
- >
- > struct Foo: public Bar
- > {
- > public:
- > // some public members
- > private:
- > // some private members
- > };
- >
- > ...since the protection of all members is explicit. And, the fact that
- > Foo is a struct or class doesn't affect the inheritance from Bar (that
- > is, whatever public interface Bar defines becomes part of Foo's, in
- > either case, as expected).
- >
- > Structs can inherit from other structs and classes and can be inherited
- > by other structs and classes. Structs can have virtual functions, member
- > operators, constructors and destructors, you can create template structs,
- > and so on.
- >
- > In other words, structs *are* classes.
-
- And so, the "class" keyword was not necessary to C++ !!! It is
- for only an historical (and cultural) reason that classes in C
- can be specified with the "class" keyword. Historically,
- structs in first C++ beta versions did not allow for methods
- and constructor (because classes complicated the C++ to C
- conversion, and made compilation time and generated volume
- of C source much bigger). However, the need of costructors
- for PODS structs relaxed this rule, and structs gained all
- the capabilities of classes, for clarity and simplicity !
-
- In most sources,
- the public members are specified at the beginning of the class
- definition so using "struct" instead of "class" avoids to
- explicitly use the "public:" access modifier and saves 7
- characters !!!
-
- >
- > Regards,
- > -------------------------------------------------------------------------
- > Matt Arnold | | ||| | |||| | | | || ||
- > marnold@netcom.com | | ||| | |||| | | | || ||
- > Boston, MA | 0 | ||| | |||| | | | || ||
- > 617.389.7384 (h) 617.576.2760 (w) | | ||| | |||| | | | || ||
- > C++, MIDI, Win32/95 developer | | ||| 4 3 1 0 8 3 || ||
- > -------------------------------------------------------------------------
-
-